package cn.zdsoft.configuration.core.web;

import cn.zdsoft.configuration.core.api.IConfigClient;

import javax.servlet.ServletContext;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by wangdongdong on 2018/5/14.
 */
public class DefaultWebConfigContext implements WebConfigContext {

    private ServletContext servletContext;

    private IConfigClient configClient;

    private Map<String, Object> attrMap = new ConcurrentHashMap<>();

    @Override
    public ServletContext getServletContext() {
        return servletContext;
    }

    @Override
    public void setServletContext(ServletContext servletContext) {
        this.servletContext = servletContext;
    }

    @Override
    public void setConfigClient(IConfigClient configClient) {
        this.configClient = configClient;
    }

    @Override
    public IConfigClient getConfigClient() {
        return configClient;
    }

    @Override
    public void setAttribute(String str, Object obj) {
        attrMap.put(str, obj);
    }

    @Override
    public Object getAttribute(String str) {
        ServletContext sc = getServletContext();
        if (sc == null) {
            throw new IllegalStateException();
        }
        Object o = sc.getAttribute(str);
        if (o != null) {
            return o;
        }
        return attrMap.get(str);
    }
}
